package com.example.ll_video.service;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.RemoteViews;
import android.widget.Toast;


import androidx.annotation.RequiresApi;
import androidx.core.content.FileProvider;

import com.example.ll_video.activity.NewMainActivity;
import com.example.ll_video.utils.DownloadUtil;
import com.example.ll_video.utils.ToastUtils;
import com.example.ll_video.R;

import java.io.File;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


/**
 * @author lwl
 * 版本更新服务
 */
public class UpdateService extends Service {
    public static int APP_VERSION_OLDER = 1;

    public static String SERVICE_BC = "service_broadcast";
    IntentFilter intentFilter;
    MyReceiver myReceiver;
    final ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);

    private Handler mHandler = new Handler(Objects.requireNonNull(Looper.myLooper())) {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 10:
                    String appFile = (String) msg.obj;
                    if (!TextUtils.isEmpty(appFile))
                        install(appFile);
                    break;
                case 11:

                    break;
                case 12:
                    Toast.makeText(UpdateService.this, R.string.app_upgrade_download_fail, Toast.LENGTH_LONG).show();
                    break;
                default:
                    break;
            }
        }

    };

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        intentFilter = new IntentFilter(SERVICE_BC);
        myReceiver = new MyReceiver();
        registerReceiver(myReceiver, intentFilter);
        return super.onStartCommand(intent, flags, startId);
    }

    class MyReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
//            String fileName = intent.getStringExtra("flag");
//            String url = intent.getStringExtra("url");
            String fileName = "wifilook.apk";
            String url = "https://imtt.dd.qq.com/16891/apk/278C27472F2CFD1CA001609443C8CB94.apk?fsname=com.alex.lookwifipassword_4.1.7_202009161.apk&csr=1bbd";
            startDanloadXutils(url, fileName);
        }
    }

    private long time;

    private void startDanloadXutils(String url, String fileName) {
        ToastUtils.showTextToast(getApplicationContext(), "开始下载");
        fixedThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                time = System.currentTimeMillis();
                String appPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath() + "/" + time;

                DownloadUtil.get().download(url, appPath, fileName,
                        new DownloadUtil.OnDownloadListener() {
                            @Override
                            public void onDownloadSuccess(File file) {

                                //下载完成进行相关逻辑操作
                                Message msg = mHandler.obtainMessage();
                                msg.what = 10;
                                msg.obj = file.getPath();
                                mHandler.sendMessageDelayed(msg, 1000);

                            }

                            @Override
                            public void onDownloading(int progress) {
                              /*  Message msg = mHandler.obtainMessage();
                                msg.what = 11;
                                msg.obj = progress;
                                mHandler.sendMessage(msg);*/
                            }

                            @Override
                            public void onDownloadFailed(Exception e) {
                                //下载异常进行相关提示操作
                                Message msg = mHandler.obtainMessage();
                                msg.what = 12;
                                msg.obj = e;
                                mHandler.sendMessage(msg);
                            }
                        });
            }
        });

    }


    public void install(String apkFile) {
        Intent intent = new Intent(NewMainActivity.SERVICE_BC);
        intent.putExtra("flag", "installApp");
        intent.putExtra("filePath", apkFile);
        sendBroadcast(intent);

    }

}
